Level1 = Class.extend({
	init: function(context, images, enemyShotAudio) {
		this.context = context;
		this.image = images;
                this.enemyShotAudio = enemyShotAudio;
                
		this.deadEnemy = false;
		
		this.interval = 100;
		this.firstShootFromEnemy = 2000;
		this.secondShootFromEnemy = 3200;

		this.initDateEnemy ;
		this.nowDate ;
		this.deadEnemyDate;
		
		this.healthBar = new Bar(this.context, this.image);
		this.playerHealth = 1;
		this.playerDead = false;
	},
		
	generateRandomEnemies: function() {
		var self = this;
		
	    var randomNumber = Math.floor(Math.random()*3);
	    this.Shoot = 0;
	    	    	
		switch (randomNumber) {
			case 0:
				self.enemy = new Enemy1(self.context, self.image[3]);		
				self.activEnemyPosition = self.enemy.initEnemy(self.Shoot);				
				var d = new Date();
				this.initDateEnemy = d.getTime();
				break;
			case 1:
				self.enemy = new Enemy2(self.context, self.image[1]);
				self.activEnemyPosition = self.enemy.initEnemy(self.Shoot);
				var d = new Date();
				this.initDateEnemy = d.getTime();
				break;
			case 2:
				self.enemy = new Enemy3(self.context, self.image[2])
				self.activEnemyPosition = self.enemy.initEnemy(self.Shoot);
				var d = new Date();
				this.initDateEnemy = d.getTime();
				break;
			default:
				break;
			}
		
	},
	

	initEnemyLevel: function(newCount, apperNewEnemy) {
		var self = this;
		
		
		self.generateRandomEnemies(self);
		
        this.chooseEnemyInterval = setInterval(function() { 
        	var d = new Date();
        	this.nowDate = d.getTime();
        	
        	if(self.playerHealth >= 10){
        		self.playerDead = true;
        	}
        	if(self.playerHealth < 10){
        		//init heath bar
        		self.healthBar.initHealthBar(self.playerHealth);	
        		
        		if(self.Shoot ==0){
		        	if( (this.nowDate - self.initDateEnemy >= self.firstShootFromEnemy) && 
			        		(this.nowDate - self.initDateEnemy <= self.firstShootFromEnemy+100) ){
		        		
		        		self.enemy.drawShooterEnemy(); 
	                    self.enemyShotAudio.load();
	                    self.enemyShotAudio.play();
		                        
                                    if (self.randomShoot()){
                                    self.context.drawImage(self.image[21],
                                                            0, 0,
                                                            800,1000,
                                                            250, 100,
                                                            450, 500);
                                    self.playerHealth ++;
		        		}
		        	};
        		
		        	if((this.nowDate - self.initDateEnemy) >= (self.firstShootFromEnemy+200) && 
			        	(this.nowDate - self.initDateEnemy) <= (self.firstShootFromEnemy+300)){
                                                self.context.clearRect(250, 100,
                                                                        450, 500);
		        			self.enemy.drawWalkEnemy();
		        	};
	        	
		        	if( (this.nowDate - self.initDateEnemy) >= (self.secondShootFromEnemy ) && 
			        	(this.nowDate - self.initDateEnemy) <= (self.secondShootFromEnemy + 100)){
		        			self.enemy.drawShooterEnemy();
		        			self.enemyShotAudio.load();
                                                self.enemyShotAudio.play();
		                    
                                    if (self.randomShoot()){
                                        self.context.drawImage(self.image[21],
                                                            0, 0,
                                                            800,1000,
                                                            250, 100,
                                                            450, 500);
                                        self.playerHealth ++;            
                                    }
		        	};
	        	
		        	if( (this.nowDate - self.initDateEnemy) >= (self.secondShootFromEnemy + 200) && 
			        	(this.nowDate - self.initDateEnemy) <= (self.secondShootFromEnemy + 300)){
		        		self.context.clearRect(250, 100,
                                                                        450, 500);
		        		self.enemy.drawWalkEnemy();
	                    
		        	};
	        	
		        	if((this.nowDate - self.initDateEnemy) >= (self.secondShootFromEnemy + 1000) && 
			        		(this.nowDate - self.initDateEnemy) <= (self.secondShootFromEnemy + 1100)){    		
		        		self.context.clearRect(120, 0, 860, 520);
		        		self.generateRandomEnemies(self);
		        	};
        		};    	
	        	
        		if((this.nowDate - self.deadEnemyDate) >= 650 && (this.nowDate - self.deadEnemyDate <= 750)){
            		self.generateRandomEnemies(self);
            	}
	        	
        	};
		}, self.interval);
    
	},
	
	randomShoot: function() {
		var randomNumber = Math.floor(Math.random()*3);
		if(randomNumber == 2){
			return false;
		}
		return true;
	},
	
	checkedPosition: function(mouseCoordinate,theCount) {
		var self = this;
		this.mouseCoordinateX = mouseCoordinate[0];
		this.mouseCoordinateY = mouseCoordinate[1];
       		
        if(this.mouseCoordinateX > self.activEnemyPosition[0] && this.mouseCoordinateX < self.activEnemyPosition[2] &&
        	this.mouseCoordinateY > self.activEnemyPosition[1] && this.mouseCoordinateY < self.activEnemyPosition[3] ){
        	
        	this.Shoot++;
 
                self.enemy.dyingEnemy(self.Shoot);

                if(theCount <= 1){    
                clearInterval(self.chooseEnemyInterval);  
            };
			
         };
      
	      if (this.Shoot === 1){
	  		this.deadEnemy = true; 
	  		var d = new Date();
	  		this.deadEnemyDate = d.getTime();
	  	   } else{
	  		this.deadEnemy = false;
	  	   };
	},
	
	returnDeadEnemy : function() {
		return this.deadEnemy;
	},
	
	returnDeadPlayer : function() {
		return this.playerDead;
	}
	
})